package com.hqyj.smvc.controller;

import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping(value = "employeeLogin")
    public String employeeLogin(String username, String password, Model model, HttpSession session){
        String str = "";
        if(!"".equals(username) && !"".equals(password)){
            Employee employee = new Employee();
            employee.setEmployee_username(username);
            employee.setEmployee_password(password);
            System.out.println(employee);
            Employee loginEmployee = loginService.loginEmployee(employee);
            if(loginEmployee != null){
                String name = loginEmployee.getEmployee_name();
                session.setAttribute("name", name);
                str = "/WEB-INF/welcome/index";
            }else {
                model.addAttribute("error", "用户名或密码错误！");
                return "/login";
            }
        }else {
            model.addAttribute("error", "用户名或密码输入有误！");
            return "/login";
        }
        return str;
    }
}
